package com.zombie.effect;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.RayCastCallback;
import com.zombie.ResourceManager;
import com.zombie.logic.object.Bullet;
import com.zombie.util.Utils;

public class RicochetEffect extends AbstractEffect implements RayCastCallback {

	float angle,normalAngle, velocity;
	
	float alphaStart  = 1.0f;
	float alphaFinish  = 1.0f;
	public Color start;
	public Color finish;
	Vector2 newPosition = new Vector2();
	
	public RicochetEffect(Body b1, Body b2,Bullet b){
		position = new Vector2(b1.getPosition().x,b1.getPosition().y);
		angle = (float) b.angle;
		velocity = b.getVelocity();
		setFullLifeTime(100);
		start = new Color(Color.WHITE.r,Color.WHITE.g,Color.WHITE.b,0.6f);
		finish = new Color(Color.WHITE.r,Color.WHITE.g,Color.WHITE.b,0.6f); 
		
	}
	
	@Override
	public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal,
			float fraction) {

		normalAngle = (float) Math.toDegrees(Math.atan2(point.y-(point.y+normal.y*32), point.x-(point.x+normal.x*32)));
		if (normalAngle < 0)
			normalAngle = normalAngle+360;
		
		if (Math.abs(normalAngle-angle) < 15)
			return 0;
		if (Utils.rnd.nextInt(3) != 0)
			return 0;
		float a = normalAngle-(angle-normalAngle);

		angle = a;
		newPosition.x = point.x;
		newPosition.y = point.y;
//		setShape(new Line(newPosition.x,newPosition.y,newPosition.x-1,newPosition.y));
		ResourceManager.getSound("ric_"+ Utils.rnd.nextInt(2)).play();
		return 0;
	}

	@Override
	public void update(int delta) {
		if (getShape() == null){
			setLifeTime(0);
			return;
		}
		setLifeTime(getLifeTime()-delta);
		
		float x1 = velocity*delta*MathUtils.cos(MathUtils.degRad*angle);
		float y1 = velocity*delta*MathUtils.sin(MathUtils.degRad*angle);
		newPosition.x-=x1;
		newPosition.y-=y1;
//		Line l = (Line) getShape();
//		l.set(newPosition.x, newPosition.y, l.getX2(), l.getY2());
		
		if (getLifeTime() >= getFullLifeTime()/2) {
			float onePercent = getFullLifeTime()/2/100f;
			float percents = getLifeTime()/2/onePercent;
			alphaStart = percents*0.01f;
		}

		float onePercent = getFullLifeTime()/100f;
		float percents = getLifeTime()/onePercent;
		alphaFinish = percents*0.01f;
		
		
		start = new Color(start.r,start.g,start.b,alphaStart);
		finish = new Color(finish.r,finish.g,finish.b,alphaStart);

	}

	@Override
	public void draw(SpriteBatch batch, ShapeRenderer shapeBatch) {
		if (getShape() == null){
			setLifeTime(0);
			return;
		}
		
//		GradientFill gf = new GradientFill(new Vector2f(getShape().getPoint(0)[0],getShape().getPoint(0)[1]), finish, new Vector2f(getShape().getPoint(1)[0],getShape().getPoint(1)[1]),start , false);
//		g.draw(getShape(),gf);
//		g.setColor(Color.green);
//		Line l = (Line) getShape();
//
//		g.drawLine(l.getX2(), l.getY2(), rightNormal.x, rightNormal.y);
//		g.setColor(Color.blue);
//		g.drawLine(l.getX2(), l.getY2(), leftNormal.x, leftNormal.y);
//		g.setColor(Color.red);
//		g.drawLine(l.getX2(), l.getY2(), normal.x, normal.y);
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub

	}
}
